package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.dto.ProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.ProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /*分页查询*/
    @Override
    public PageResponse<NursingPlanVo> findAllByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlan> nursingPlans =nursingPlanMapper.findAllByPage(name,status);
        PageResponse<NursingPlanVo> of = PageResponse.of(nursingPlans, NursingPlanVo.class);
        return of;
    }
    /*根据id查询*/
    @Override
    public NursingPlanVo selectById(Long id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.findById(id);
        List<ProjectPlanVo> projectPlanVos = nursingPlanMapper.selectById(id);
        nursingPlanVo.setProjectPlans(projectPlanVos);
        return nursingPlanVo;
    }
    /*新增护理计划*/
    @Override
    public void save(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(0);
        nursingPlanMapper.save(nursingPlan);

        Long id = nursingPlan.getId();
        List<NursingProjectPlan> projectPlans = nursingPlan.getProjectPlans();

         for (NursingProjectPlan nursingProjectPlan : projectPlans) {
             nursingProjectPlan.setPlanId(id);
             nursingPlanMapper.saveProjectPlan(nursingProjectPlan);
         }

    }
    /*根据id修改护理计划*/
    @Override
    public void updateById(NursingPlanDto nursingPlanDto) {


        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.updateById(nursingPlan);   //通过id修改unrsing_plan中的护理计划名称
       //通过请求中的计划id通过循环获取计划中中间表的id  修改计划下的unrsing_project_plan的数据

        nursingProjectMapper.deletePlanById(nursingPlanDto.getId());

        List<ProjectPlanDto> projectPlans1 = nursingPlanDto.getProjectPlans();


        Long id = nursingPlan.getId();
        List<NursingProjectPlan> projectPlans = nursingPlan.getProjectPlans();
        for (NursingProjectPlan nursingProjectPlan : projectPlans) {
            nursingProjectPlan.setPlanId(id);
            nursingPlanMapper.saveProjectPlan(nursingProjectPlan);
        }

    }
    /*根据id删除*/
    @Override
    public void deleteById(Long id) {
      nursingPlanMapper.deleteById(id);
    }
    /*通过id修改状态*/
    @Override
    public void updateStatusById(Long id, Integer status) {
        nursingPlanMapper.updateStatusById(id,status);
    }
}

